Skip to content

[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] [Intel] Backport bugfixes for SRF LBR branch counter#838

Merged
opsiff merged 3 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:linux-6.6.y
Jun 2, 2025
Merged

[Deepin-Kernel-SIG] [linux 6.6-y] [Upstream] [Intel] Backport bugfixes for SRF LBR branch counter#838
opsiff merged 3 commits into
deepin-community:linux-6.6.yfrom
Avenger-285714:linux-6.6.y

Conversation

@Avenger-285714
Copy link
Copy Markdown
Member

@Avenger-285714 Avenger-285714 commented Jun 2, 2025

Fix: #828

Suggested-by: Wentao Guan guanwentao@uniontech.com

Summary by Sourcery

Backport Intel SRF LBR branch counter bug fixes to x86 perf event handling in kernel 6.6

Bug Fixes:

  • Prevent branch stack setup for non-sampling events in SAMPLE_READ path
  • Fix branch counters group detection for non-x86 events

Enhancements:

  • Introduce check_leader_group helper, relocate is_x86_event declaration to header, and refactor is_branch_counters_group to use the new helper

Kan Liang added 2 commits June 2, 2025 12:59
[ Upstream commit 75aea4b ]

A warning in intel_pmu_lbr_counters_reorder() may be triggered by below
perf command.

perf record -e "{cpu-clock,cycles/call-graph="lbr"/}" -- sleep 1

It's because the group is mistakenly treated as a branch counter group.

The hw.flags of the leader are used to determine whether a group is a
branch counters group. However, the hw.flags is only available for a
hardware event. The field to store the flags is a union type. For a
software event, it's a hrtimer. The corresponding bit may be set if the
leader is a software event.

For a branch counter group and other groups that have a group flag
(e.g., topdown, PEBS counters snapshotting, and ACR), the leader must
be a X86 event. Check the X86 event before checking the flag.
The patch only fixes the issue for the branch counter group.
The following patch will fix the other groups.

There may be an alternative way to fix the issue by moving the hw.flags
out of the union type. It should work for now. But it's still possible
that the flags will be used by other types of events later. As long as
that type of event is used as a leader, a similar issue will be
triggered. So the alternative way is dropped.

Fixes: 3374491 ("perf/x86/intel: Support branch counters logging")
Closes: https://lore.kernel.org/lkml/20250412091423.1839809-1-luogengkun@huaweicloud.com/
Reported-by: Luo Gengkun <luogengkun@huaweicloud.com>
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/20250424134718.311934-2-kan.liang@linux.intel.com
[ Backport from v6.15 ]
Suggested-by: Wentao Guan <guanwentao@uniontech.com>
Signed-off-by: WangYuli <wangyuli@uniontech.com>
[ Upstream commit ef493f4 ]

The BPF subsystem may capture LBR data on a counting event. However, the
current implementation assumes that LBR can/should only be used with
sampling events.

For instance, retsnoop tool ([0]) makes an extensive use of this
functionality and sets up perf event as follows:

	struct perf_event_attr attr;

	memset(&attr, 0, sizeof(attr));
	attr.size = sizeof(attr);
	attr.type = PERF_TYPE_HARDWARE;
	attr.config = PERF_COUNT_HW_CPU_CYCLES;
	attr.sample_type = PERF_SAMPLE_BRANCH_STACK;
	attr.branch_sample_type = PERF_SAMPLE_BRANCH_KERNEL;

To limit the LBR for a sampling event is to avoid unnecessary branch
stack setup for a counting event in the sample read. Because LBR is only
read in the sampling event's overflow.

Although in most cases LBR is used in sampling, there is no HW limit to
bind LBR to the sampling mode. Allow an LBR setup for a counting event
unless in the sample read mode.

Fixes: 85846b2 ("perf/x86: Add PERF_X86_EVENT_NEEDS_BRANCH_STACK flag")
Closes: https://lore.kernel.org/lkml/20240905180055.1221620-1-andrii@kernel.org/
Reported-by: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Signed-off-by: Kan Liang <kan.liang@linux.intel.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Tested-by: Andrii Nakryiko <andrii@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20240909155848.326640-1-kan.liang@linux.intel.com
[ Backport from v6.12 ]
Suggested-by: Wentao Guan <guanwentao@uniontech.com>
Signed-off-by: WangYuli <wangyuli@uniontech.com>
@Avenger-285714 Avenger-285714 requested a review from Copilot June 2, 2025 05:06
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 2, 2025

Reviewer's Guide

Backport SRF LBR branch counter bugfixes by exposing the is_x86_event API, centralizing leader flag checks into a helper, refactoring branch-group detection, and tightening branch-stack flag assignment to skip count-only sample reads.

Sequence Diagram for Updated is_branch_counters_group Logic

sequenceDiagram
    participant Caller
    participant is_branch_counters_group
    participant check_leader_group
    participant is_x86_event

    Caller->>is_branch_counters_group: event
    is_branch_counters_group->>check_leader_group: event->group_leader, PERF_X86_EVENT_BRANCH_COUNTERS
    check_leader_group->>is_x86_event: leader
    is_x86_event-->>check_leader_group: bool (is_x86_event_result)
    alt is_x86_event_result is true
        check_leader_group->>check_leader_group: Check leader->hw.flags & flags
    end
    check_leader_group-->>is_branch_counters_group: bool (check_leader_group_result)
    is_branch_counters_group-->>Caller: bool (final_result)
Loading

Class Diagram of Changes to X86 Performance Event Handling

classDiagram
    class X86EventFunctions {
        <<Module: x86 Events>>
        +is_x86_event(event: perf_event*) bool // Exposed
        +check_leader_group(leader: perf_event*, flags: int) bool // New helper
        +is_branch_counters_group(event: perf_event*) bool // Modified
        +intel_pmu_hw_config(event: perf_event*) int // Modified
    }

    class perf_event {
        <<struct>>
        +hw_flags : int
        +attr_sample_type : int
        +group_leader : perf_event*
    }

    X86EventFunctions ..> perf_event : Uses
Loading

File-Level Changes

Change Details Files
Exposed is_x86_event function for external use
  • Changed is_x86_event from static inline to external
  • Added its prototype in perf_event.h
arch/x86/events/core.c
arch/x86/events/perf_event.h
Introduced check_leader_group helper and refactored branch-group detection
  • Defined new inline check_leader_group for leader flags
  • Replaced is_branch_counters_group logic to call the helper
arch/x86/events/perf_event.h
Refined branch stack setup to skip count-only sample reads
  • Wrapped branch-stack flag assignment in expanded conditional
  • Excluded setup when PERF_SAMPLE_READ is not requested
arch/x86/events/intel/core.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from avenger-285714. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@Avenger-285714 Avenger-285714 requested a review from opsiff June 2, 2025 05:07
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request backports bugfixes for Intel SRF LBR branch counter handling in the x86 perf event subsystem.

  • Introduces is_x86_event and check_leader_group to guard branch-counter queries on non-x86 events
  • Refactors is_branch_counters_group to use the new helper
  • Adjusts branch stack setup logic to avoid unintended counting behavior during SAMPLE_READ
  • Promotes is_x86_event from static inline to an external function

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
arch/x86/events/perf_event.h Declare is_x86_event externally and add check_leader_group helper
arch/x86/events/intel/core.c Update branch stack setup conditions with nested logic
arch/x86/events/core.c Move is_x86_event out of static inline and into external linkage
Comments suppressed due to low confidence (1)

arch/x86/events/perf_event.h:115

  • The flags parameter is declared as int but hw.flags is likely an unsigned long bitmask; consider changing flags to unsigned long to match and avoid type mismatches.
static inline bool check_leader_group(struct perf_event *leader, int flags)

Comment on lines +4008 to +4009
!(event->attr.sample_type & PERF_SAMPLE_READ))
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding braces around the inner if block to improve readability and prevent accidental inclusion of additional statements in future edits.

Suggested change
!(event->attr.sample_type & PERF_SAMPLE_READ))
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
!(event->attr.sample_type & PERF_SAMPLE_READ)) {
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
}

Copilot uses AI. Check for mistakes.
return is_metric_event(event) || is_slots_event(event);
}

int is_x86_event(struct perf_event *event);
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a brief comment describing what constitutes an "x86 event" so maintainers understand the criteria without digging into the implementation.

Copilot uses AI. Check for mistakes.
Comment thread arch/x86/events/core.c
}

static inline int is_x86_event(struct perf_event *event)
int is_x86_event(struct perf_event *event)
Copy link

Copilot AI Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Since is_x86_event returns a boolean-like result, consider changing its return type to bool for clarity and consistency with other predicate functions.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Avenger-285714 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

if (needs_branch_stack(event) && is_sampling_event(event))
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
if (needs_branch_stack(event)) {
/* Avoid branch stack setup for counting events in SAMPLE READ */
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Clarify SAMPLE_READ logic in comment

Consider updating the comment to clarify that branch stack setup is skipped only when the event is not a sampling event and PERF_SAMPLE_READ is set.

Suggested change
/* Avoid branch stack setup for counting events in SAMPLE READ */
/* Skip branch stack setup only for non-sampling events with PERF_SAMPLE_READ set */

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

关键摘要:

  • is_x86_event 函数从 static inline 变为 int,可能会影响其他文件的调用,需要确认是否有其他文件依赖此函数的 inline 属性。
  • intel_pmu_hw_config 函数中,对 event->hw.flags 的赋值操作增加了对 PERF_SAMPLE_READ 标志的检查,这是一个合理的改动,可以避免不必要的分支栈设置。

是否建议立即修改:

  • 是,需要确认 is_x86_event 函数的修改不会导致其他文件编译错误,并且确保这个修改不会影响现有的功能。同时,建议添加相应的注释来解释为什么需要这个改动。

@opsiff opsiff merged commit 928d59e into deepin-community:linux-6.6.y Jun 2, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants